﻿@inject IUserService UserService
@inject DialogService DialogService
@inject ExampleJsInterop JsInterop

<RadzenStack AlignItems="AlignItems.Start" Wrap="FlexWrap.Wrap">
    <RadzenStack class="rz-mb-2">
        <ImportFormPartial @ref="_importForm" ExportTemplateHandler="DownloadTemplateHandler"></ImportFormPartial>
    </RadzenStack>
    <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End">
        <RadzenButton ButtonStyle="ButtonStyle.Light" Text="Cancel" Variant="Variant.Flat" Click="@CancelButtonClick" />
        <RadzenButton ButtonStyle="ButtonStyle.Primary" ButtonType="ButtonType.Button" Icon="save" Text="Save" Variant="Variant.Flat"
                      Click="SaveButtonClick" />
    </RadzenStack>
</RadzenStack>

@code {

    private ImportFormPartial _importForm = new();

    void CancelButtonClick()
    {
        DialogService.Close();
    }

    async Task SaveButtonClick()
    {
        // bool isValid = await _importForm.Validate();
        // if (isValid)
        // {
        //     var importFormImportFormInfo = _importForm.ImportFormInfo;

        //     var result = await UserService.ImportUsers(importFormImportFormInfo.ImportType, importFormImportFormInfo.UploadFile.Name,
        //         importFormImportFormInfo.UploadFile.OpenReadStream());

        //     if (result.Succeeded)
        //     {

        //     }
        //     else
        //     {

        //     }
        // }
    }


    async Task DownloadTemplateHandler()
    {
        var response = await UserService.ExportTemplate();
        var fileName = HttpResponseMessageHelper.GetFileName(response);
        var stream = await response.Content.ReadAsStreamAsync();
        await JsInterop.DownloadFileFromStream(stream, fileName);
    }

}